package com.qianji.petmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.Query;
import com.qianji.petmall.product.dao.BrandDao;
import com.qianji.petmall.product.dao.CategoryBrandRelationDao;
import com.qianji.petmall.product.entity.BrandEntity;
import com.qianji.petmall.product.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //获取key
        String key = (String) params.get("key");
        System.out.println(key);
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void updateDetail(BrandEntity brand) {
        String name = brand.getName();
        Long brandId = brand.getBrandId();
        baseMapper.updateById(brand);
        if (StringUtils.isNotEmpty(name)){
            categoryBrandRelationDao.updateBrand(brandId,name);
        }
    }

    @Override
    public List<BrandEntity> getBrandByBrandId(Long brandId) {
        List<BrandEntity> brand_id = this.list(new QueryWrapper<BrandEntity>().eq("brand_id", brandId));
        return brand_id;
    }

}